package com.antoco.operation.bigscreen.utils;

import com.antoco.operation.bigscreen.model.entity.TjIns;
import com.antoco.operation.bigscreen.model.entity.TjZt;
import com.antoco.operation.bigscreen.repository.TjInsRepository;
import com.antoco.operation.bigscreen.repository.TjZtRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.persistence.Table;
import java.sql.Timestamp;
import java.util.List;

/**
 * <p>
 * 实体工具类
 * </p>
 *
 * @Author yejx
 * @Date 2020/11/29
 */
@Component
public class BeanUtils {

    @Autowired
    TjZtRepository tjZt;
    private static TjZtRepository tjZtRepository;


    @Autowired
    TjInsRepository tjins;
    private static TjInsRepository tjInsRepository;

    @PostConstruct
    public void init() {
        tjZtRepository = tjZt;
        tjInsRepository = tjins;
    }

    /**
     *保存统计实例，更新统计主题
     * @param clazz
     * @return
     */
    public static TjIns saveTjIns(Class<?> clazz) {
        //获取当前时间戳
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        //获取实体对应的表名
        String tableName = clazz.getAnnotation(Table.class).name();
        //根据表名获取对应的统计主题记录
        List<TjZt> list = tjZtRepository.findByTjInsDetailTableName(tableName);
        TjZt tjZt = list.get(0);
        //封装统计实例
        TjIns tjIns = new TjIns();
        tjIns.setId(UuidUtil.getUuid());
        tjIns.setTjZtId(tjZt.getId());
        tjIns.setGenType(null);
        tjIns.setEtlTaskId(null);
        tjIns.setBusiActId(null);
        tjIns.setGenTime(timestamp);
        tjIns.setRecState(null);
        //更新统计主题lastTjInsId
        tjZt.setLastTjInsId(tjIns.getId());
        tjZtRepository.save(tjZt);
        //保存/更新统计实例，返回统计实例
        return tjInsRepository.save(tjIns);
    }
}
